{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Testing extract_relevance_range_from_docs_with_conversation Function\n",
    "\n",
    "This notebook demonstrates how to use and test the `extract_relevance_range_from_docs_with_conversation` function from the `token_limiter.py` module."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\u001b[32m2024-12-19 22:42:23.348\u001b[0m | \u001b[1mINFO    \u001b[0m | \u001b[36mbyzerllm.utils.connect_ray\u001b[0m:\u001b[36mconnect_cluster\u001b[0m:\u001b[36m48\u001b[0m - \u001b[1mJDK 21 will be used (/Users/allwefantasy/.auto-coder/jdk-21.0.2.jdk/Contents/Home)...\u001b[0m\n",
      "2024-12-19 22:42:23,419\tINFO worker.py:1564 -- Connecting to existing Ray cluster at address: 127.0.0.1:6379...\n",
      "2024-12-19 22:42:23,442\tINFO worker.py:1740 -- Connected to Ray cluster. View the dashboard at \u001b[1m\u001b[32m127.0.0.1:8265 \u001b[39m\u001b[22m\n"
     ]
    }
   ],
   "source": [
    "import byzerllm\n",
    "\n",
    "# Initialize ByzerLLM\n",
    "llm = byzerllm.ByzerLLM.from_default_model(\"deepseek_chat\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Create Sample Data\n",
    "\n",
    "Let's create some sample conversations and documents to test the relevance range extraction."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Sample conversation\n",
    "conversations = [\n",
    "    {\"role\": \"user\", \"content\": \"What are the main methods in TokenLimiter class?\"},\n",
    "    {\"role\": \"assistant\", \"content\": \"The TokenLimiter class provides methods for managing token limits in text processing.\"},\n",
    "    {\"role\": \"user\", \"content\": \"Can you explain the initialization parameters?\"}\n",
    "]\n",
    "\n",
    "# Sample document\n",
    "document = \"\"\"\n",
    "class TokenLimiter:\n",
    "    def __init__(\n",
    "        self,\n",
    "        count_tokens: Callable[[str], int],\n",
    "        full_text_limit: int,\n",
    "        segment_limit: int,\n",
    "        buff_limit: int,\n",
    "        llm:ByzerLLM,\n",
    "        disable_segment_reorder: bool,\n",
    "    ):\n",
    "        self.count_tokens = count_tokens\n",
    "        self.full_text_limit = full_text_limit\n",
    "        self.segment_limit = segment_limit\n",
    "        self.buff_limit = buff_limit\n",
    "        self.llm = llm\n",
    "\n",
    "    def limit_tokens(self, relevant_docs: List[SourceCode]):\n",
    "        pass\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Import and Test the Function\n",
    "\n",
    "Now let's import and test the extract_relevance_range_from_docs_with_conversation function."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Relevance Range Result:\n",
      "[{\"start_line\": 1, \"end_line\": 8}]\n"
     ]
    }
   ],
   "source": [
    "from autocoder.rag.token_limiter import TokenLimiter\n",
    "def count_tokens(text:str):\n",
    "    return 0\n",
    "token_limiter = TokenLimiter(llm=llm,count_tokens=count_tokens,full_text_limit=1000,segment_limit=1000,buff_limit=1000,disable_segment_reorder=False)\n",
    "# Test relevance range extraction\n",
    "result = token_limiter.extract_relevance_range_from_docs_with_conversation.with_llm(llm).run(conversations, [document])\n",
    "print(f\"Relevance Range Result:\\n{result}\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
